/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author ben
 */
public class controller_DoanTau {

    public List<model_DoanTau> LoadList() throws Exception {

        try {
            DataProvider conn = new DataProvider();
            conn.Open();
            List<model_DoanTau> lst = new ArrayList<>();
            String query = "SELECT * FROM doan_tau";
            //Statement st = conn.createStatement();
            ResultSet rs = conn.ExecuteQuery(query);

            while (rs.next()) {
                model_DoanTau dt = new model_DoanTau();
                dt.setMADT(rs.getString("MaDT"));
                dt.setTENDT(rs.getString("TenDT"));
                dt.setHANGSX(rs.getString("HangSX"));
                dt.setNGAYVANHANH(rs.getDate("NgayVanHanh"));
                dt.setLOAITAU(rs.getString("LoaiTau"));

                lst.add(dt);
            }
            rs.close();
            conn.Close();
            return lst;

        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
        return null;
    }

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    //DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

    public void Xoa_DoanTau(String madt) {
        DataProvider conn = new DataProvider();
        conn.Open();
        String query = String.format("DELETE FROM doan_tau WHERE MaDT = '%s'", madt);
        conn.ExecuteUpdate(query);
    }

    public int Them_DoanTau(model_DoanTau dt) {
        DataProvider conn = new DataProvider();
        conn.Open();
        String query = String.format("INSERT INTO doan_tau VALUES ('%s','%s','%s','%s','%s')",
                dt.getMADT(),
                dt.getTENDT(),
                dt.getHANGSX(),
                df.format(dt.getNGAYVANHANH()),
                dt.getLOAITAU());
        return conn.ExecuteUpdate(query);
    }

    public int CapNhat_DoanTau(model_DoanTau dt) {
        DataProvider conn = new DataProvider();
        conn.Open();

        String query = String.format("UPDATE doan_tau SET TenDT = '%s', HangSX = '%s', NgayVanHanh = '%s', LoaiTau= '%s' where MaDT = '%s'",
                dt.getTENDT(),
                dt.getHANGSX(),
                df.format(dt.getNGAYVANHANH()),
                dt.getLOAITAU(),
                dt.getMADT());

        return conn.ExecuteUpdate(query);
    }

    public model_DoanTau Load_CapNhat_DoanTau(String madt) throws SQLException {
        DataProvider conn = new DataProvider();
        conn.Open();

        String query = String.format("SELECT * FROM doan_tau WHERE MaDT = '%s'", madt);
        ResultSet rs = conn.ExecuteQuery(query);

        model_DoanTau dt = new model_DoanTau();

        while (rs.next()) {
            dt.setMADT(rs.getString("MaDT"));
            dt.setTENDT(rs.getString("TenDT"));
            dt.setHANGSX(rs.getString("HangSX"));
            dt.setNGAYVANHANH(rs.getDate("NgayVanHanh"));
            dt.setLOAITAU(rs.getString("LoaiTau"));
        }

        return dt;
    }

    public List<model_DoanTau> TimKiem_DoanTau(String madt) throws SQLException {

        DataProvider conn = new DataProvider();
        conn.Open();

        List<model_DoanTau> lst = new ArrayList<>();
        String query = String.format("SELECT * FROM doan_tau WHERE MaDT = '%s'", madt);
        ResultSet rs = conn.ExecuteQuery(query);

        while (rs.next()) {
            model_DoanTau dt = new model_DoanTau();

            dt.setMADT(rs.getString("MaDT"));
            dt.setTENDT(rs.getString("TenDT"));
            dt.setHANGSX(rs.getString("HangSX"));
            dt.setNGAYVANHANH(rs.getDate("NgayVanHanh"));
            dt.setLOAITAU(rs.getString("LoaiTau"));

            lst.add(dt);
        }

        return lst;
    }
}
